package com.tangdi.production.mpaychl.service.impl.ksunpay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.tangdi.production.mpaychl.base.service.BaseTranService;
import com.tangdi.production.mpaychl.service.impl.ksunpay.util.MD5;
import com.tangdi.production.mpbase.constants.MsgCT;
import com.tangdi.production.mpbase.exception.TranException;
import com.tangdi.production.mpbase.util.MoneyUtils;
import com.tangdi.production.mpbase.util.ObjectUtil;

@Service("Ksunpay_SERVICE_V3")
public class KsunpayServiceImpl913 implements BaseTranService {
	private static final Logger logger = LoggerFactory.getLogger(KsunpayServiceImpl913.class);
	
	private static final String pay_notify_url = "http://ce.imbitpay.com:8083/mpcctp/sunpay/notify-url.json";
	
    private static final String Authorization_URL = "http://pay.zcapay.com/Pay_Index.html";
//    private static final String Authorization_URL = "http://www.toffpay.com/pay";
    private static final String PAY_BANK = "913";	// 网银支付
    
//    private static final String merchant_Id = "190194937";
    private static final String merchant_Id = "10167";
    public static final String key_Value = "h0cp3ki5ztx5kkwqsfn9nxtin8g45pcs";
    
	@Override
	public Map<String, Object> execute(Map<String, Object> params) throws TranException {
		try {
			String pay_bankcode = PAY_BANK;
			String pay_memberid = merchant_Id;//商户id
			String pay_orderid = "" + params.get("prdordno");//20位订单号 时间戳+6位随机字符串组成
			String pay_applydate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());;//yyyy-MM-dd HH:mm:ss
			String pay_notifyurl = pay_notify_url;//通知地址
			String pay_callbackurl = "" + params.get("frontUrl"); //回调地址
			String payAmt = ObjectUtil.getString(params.get("payAmt"));
			logger.info("支付金额[{}分]", payAmt);
			payAmt = MoneyUtils.toStrYuan(payAmt);
			logger.info("支付金额转换为元[{}]", payAmt);
			String pay_amount = payAmt;
			String pay_productname = "";
			String pay_productnum = "";
			String pay_productdesc = "";
			String pay_producturl = "";
			if (params.get("bankUsername") == null){
				throw new Exception("持卡人姓名不能为空");
			}
			if (params.get("bankName") == null){
				throw new Exception("持卡银行不能为空");
			}
			if (params.get("bankCard") == null){
				throw new Exception("持卡人卡号不能为空");
			}
			String pay_bank_username = params.get("bankUsername").toString();    
			String pay_bank_name = params.get("bankName").toString();    
			String pay_bank_card = params.get("bankCard").toString();    
			String stringSignTemp = "pay_amount="+pay_amount+"&pay_applydate="+pay_applydate+"&pay_bankcode="+pay_bankcode+"&pay_callbackurl="+pay_callbackurl+"&pay_memberid="+pay_memberid+"&pay_notifyurl="+pay_notifyurl+"&pay_orderid="+pay_orderid+"&key="+key_Value+"";
			String pay_md5sign = MD5.md5(stringSignTemp);
			
			StringBuilder returtnHtml = new StringBuilder();
			returtnHtml.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\" /></head><body><script language=\"javascript\">window.onload=function(){document.payfrm.submit();}</script>");
			returtnHtml.append("<form id=\"payfrm\" name=\"payfrm\" action=\"").append(Authorization_URL).append("\" method=\"post\" >");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_memberid\" value=\"").append(pay_memberid).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_orderid\" value=\"").append(pay_orderid).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_applydate\" value=\"").append(pay_applydate).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_bankcode\" value=\"").append(pay_bankcode).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_notifyurl\" value=\"").append(pay_notifyurl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_callbackurl\" value=\"").append(pay_callbackurl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_amount\" value=\"").append(pay_amount).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_reserved1\" value=\"").append("").append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_reserved2\" value=\"").append("").append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_reserved3\" value=\"").append("").append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_productname\" value=\"").append(pay_productname).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_productnum\" value=\"").append(pay_productnum).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_productdesc\" value=\"").append(pay_productdesc).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_producturl\" value=\"").append(pay_producturl).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_md5sign\" value=\"").append(pay_md5sign).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_bank_username\" value=\"").append(pay_bank_username).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_bank_name\" value=\"").append(pay_bank_name).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"pay_bank_card\" value=\"").append(pay_bank_card).append("\" />");
			returtnHtml.append("</form></body></html>");
			
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_PROCESSING);
			backMap.put("contentType", MsgCT.DATA_TYPE_HTML);
			backMap.put("busContent", returtnHtml.toString());
			return backMap;
		} catch (Exception e){
			logger.error("处理Ksunpay-V3支付通道异常:{}", e.getMessage(), e);
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_FAIL);
			backMap.put("msg", "支付渠道调用异常:" + e.getMessage());
			return backMap;
		}
	}
	
	public static void main(String[] args) throws TranException {
		
		Map<String, Object> params = new HashMap<>();
		params.put("payAmt", "200");
		params.put("bankUsername", "李加欢");
		params.put("bankName", "招商银行");
		params.put("bankCard", "341182198901162417");
		params.put("frontUrl", "http://58.220.30.99:8081/qrcode-starter/returnTest");
		params.put("prdordno", System.currentTimeMillis() + RandomStringUtils.randomAlphabetic(5));
		
		Map<String, Object> returnMap = new KsunpayServiceImpl913().execute(params);
		
		System.err.println(returnMap.toString());
		
		System.out.println();
		System.out.println(returnMap.get("busContent"));
	}
	
}
